<?php
namespace app\reduction\model;

use app\BaseModel;
use think\facade\Db;

class Company extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_reduction_company';


    /**
     * User: chenniang
     * Date: 2019-12-18 14:11
     * @return void
     * descrption:删除
     */
    public function companyDel($dis){

        $res = $this->where($dis)->delete();

        return $res;

    }

    /**
     * User: chenniang
     * Date: 2019-12-18 14:11
     * @return void
     * descrption:添加
     */
    public function companyAdd($data){

        $res = $this->insert($data);

        return $res;
    }


    /**
     * User: chenniang
     * Date: 2019-09-25 15:29
     * @return void
     * descption:转换插入数据
     */
    public function changeData($ids){
        $data = [];
        if(is_array($ids)){

            foreach ($ids as $k =>$v){
                $data[] = array_pop($v);
            }
        }

        return $data;
    }


    /**
     * @param $dis
     * @return array
     * 获取company
     */

    public function getId($dis){
        $data = $this
            ->alias('a')
            ->leftJoin('longbing_card_company b','a.company_id=b.id')
            ->where($dis)
            ->column('a.company_id');
        $company_id = [];
        if(!empty($data)){
            foreach ($data as $v){
                $company_id[] = $this->reset($v);
            }
        }
        return array_values($company_id);
    }

    /**
     * @param $id
     * 回显
     */
    public function reset($id,&$data=array()){
        $cate   = Db::name('longbing_card_company')->where(['id'=>$id])->find();
        if(!empty($cate)&&$cate['pid']!=$cate['id']){
            array_unshift($data,$cate['id']);
            $this->reset($cate['pid'],$data);
        }
        return $data;
    }


    /**
     * @param $dis
     * @功能说明:获取员工
     * @author chenniang
     * @DataTime: 2020-04-10 09:24
     */
    public function getStaff($dis){

        $data = $this
            ->alias('a')
            ->leftJoin('longbing_card_user_info b','a.staff_id=b.fans_id')
            ->where($dis)
            ->column('a.staff_id');

        return array_values($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-04-21 11:53
     * @功能说明:查询数量
     */
    public function companyCount($dis){

        $count = $this->where($dis)->group('company_id')->count();

        return $count;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-04-22 10:35
     * @功能说明:获取员工公司
     */
    public function getCompanyName($staff_id){


        $name = Db::name('longbing_card_user_info')
                ->alias('a')
                ->join('longbing_card_company b','a.company_id = b.id','left')
                ->join('longbing_card_company c','b.top_id = c.id','left')
                ->where(['a.fans_id'=> $staff_id])
                ->field('ifnull(c.name,b.name) as company_name,ifnull(c.logo,b.logo) as company_logo')
                ->find();

        return $name;

    }








}